<?php
class readGroup extends group {
	function __construct($id) {
		parent::__construct($id);
	}

	public function getAllFrom($id) {
		$arrResult = array();
		$query = "select g.*,(SELECT p.nombre FROM planes p WHERE (g.fk_plan = p.id)) 'plan' from grupos g WHERE (estado>0) AND (id=".$id.") AND (fk_origen=".$this->get('grupos','fk_origen').");";
		$arrResult = $this->ejecutarSelect($query);
		$i = 0;
		foreach ($arrResult as $result) {
			$key = array_keys($result);
			for ($c=0;$c<round(count($key)/2,0);$c++) {
				$this->data['grupos'][$key[$i*2+1]] = $result[$key[$i*2+1]];
				$i++;
			}
		}
		return $arrResult;
	}

	private function ejecutarSelect($query) {
		$arrResult = array();
		try {
			$registros = mysql_query($query);
			if (mysql_errno() == 1146) throw new Exception("La tabla no existe.<br>");
			else {
				$i = 0;
				$reg = mysql_fetch_array($registros);
				do {
					if ((!$reg) && ($i == 0)) throw new Exception("La tabla esta vacia.<br>");
					if (1000 < mysql_errno() && mysql_errno() < 2056) throw new Exception("Error en el SELECT.");
					else {
						$arr_keys = array_keys($reg);
						for ($ii = 0; $ii < count($arr_keys);$ii++) $arrResult[$i][$arr_keys[$ii]] = $reg[$arr_keys[$ii]];
						$i++;
					}
				} while ($reg = mysql_fetch_array($registros));
			}
		} catch (Exception $e) {
			/*echo $e->getMessage();
			echo $e->getTraceAsString();
			echo mysql_error();*/
		}
		return $arrResult;
	}
	public function getStudents($group_id) {
		$query = "SELECT a.id,a.nombre,a.email,axg.estado FROM alumnos a, alumnosxgrupo axg, grupos g WHERE (a.estado>0) AND (axg.fk_grupo=".$group_id.") AND (a.id=axg.fk_alumno) AND (axg.estado>0) AND (g.fk_origen=".$this->get('grupos','fk_origen')." AND (g.id = axg.fk_grupo));";
		$result = mysql_query($query);
		$arr_students = array();
		$i = 0;
		while ($reg = @mysql_fetch_array($result)) {
			$arr_students[$i]['id'] = $reg['id'];
			$arr_students[$i]['nombre'] = $reg['nombre'];
			$arr_students[$i]['email'] = $reg['email'];
			$arr_students[$i]['estado'] = $reg['estado'];
			$i++;
		}
		return $arr_students;
	}
}
?>